package com.xn2001.pattern;

import java.io.Serializable;

/**
 * @author 乐心湖
 * @version 1.0
 * @date 2021/11/9 19:25
 */

public class Singleton1 implements Serializable {
    private static final Singleton1 INSTANCE = new Singleton1();
    private Singleton1(){
        //防止反射破坏单例
        if (INSTANCE != null){
            throw new RuntimeException("单例对象无法重复创建实例");
        }
        System.out.println("构造私有");
    }
    public static Singleton1 getInstance() {
        return INSTANCE;
    }
    //防止反序列化破坏单例,方法名固定的
    public Object readResolve(){
        return INSTANCE;
    }
    public void method(){
        System.out.println("method");
    }
    public static void staticMethod(){
        System.out.println("static method");
    }
}
